<template>
  <div class="app-container">
    <el-form :model="queryParams"
      ref="queryForm"
      size="mini"
      :inline="true"
      v-show="showSearch"
      label-width="100px">
      <el-form-item label="订单编号"
        prop="orderNo">
        <el-input v-model="queryParams.orderNo"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="会员编号"
        prop="custNo">
        <el-input v-model="queryParams.custNo"
          placeholder="请输入会员编号"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="会员名称"
        prop="custName">
        <el-input v-model="queryParams.custName"
          placeholder="请输入会员名称"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
     
      <el-form-item label="实际付款金额"
        prop="amount">
        <el-input v-model="queryParams.amount"
          placeholder="请输入实际付款金额"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <!-- <el-form-item label="支付日期"
        prop="payTime">
        <el-date-picker v-model="queryParams.payTime"
          type="date"
          placeholder="请选择支付日期"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
          style="width: 215px"
          @keyup.enter.native="handleQuery" />
      </el-form-item> -->
      
      <el-form-item label="上级会员编号"
        prop="parentNo">
        <el-input v-model="queryParams.parentNo"
          placeholder="请输入上级会员编号"
          clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>

      <el-form-item label="支付状态"
        prop="payStatus">
        <el-select v-model="queryParams.payStatus"
          clearable
          placeholder="请选择支付状态"
          @keyup.enter.native="handleQuery"
          >
          <el-option v-for="dict in dict.type.pay_status"
            :key="dict.value"
            :label="dict.label"
            :value="parseInt(dict.value)" />
        </el-select>
      </el-form-item>
      
      <el-form-item>
        <el-button type="primary"
          icon="el-icon-search"
          @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh"
          @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10"
      class="mb8">
      <el-col :span="1.5">
        <el-button type="primary"
          plain
          icon="el-icon-plus"
          @click="handleAdd"
          v-hasPermi="['web:orders:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger"
          plain
          icon="el-icon-delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['web:orders:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning"
          plain
          icon="el-icon-download"
          @click="handleExport"
          v-hasPermi="['web:orders:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch"
        @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
      :data="ordersList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection"
        width="55"
        align="center" />
      <el-table-column label="订单编号"
        align="center"
        prop="orderNo"
        min-width="160" />
      <el-table-column label="订单类别"
        align="center"
        prop="orderType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_type"
            :value="scope.row.orderType" />
        </template>
      </el-table-column>
      <el-table-column label="会员编号"
        align="center"
        prop="custNo"
        min-width="100" />
      <el-table-column label="会员名称"
        align="center"
        prop="custName"
        min-width="120"
        show-overflow-tooltip />

      <el-table-column label="实际付款金额"
        align="center"
        prop="amount"
        min-width="120" />
      <el-table-column label="优惠券抵扣额"
        align="center"
        prop="couponMoney"
        min-width="120" />
      <el-table-column label="让利金额"
        align="center"
        prop="discount"
        min-width="100" />
      
      <el-table-column label="实收现金"
        align="center"
        prop="receipts"
        min-width="100" />
      
      <el-table-column label="支付状态"
        align="center"
        prop="payStatus"
        min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.pay_status"
            :value="scope.row.payStatus" />
        </template>
      </el-table-column>
      <el-table-column label="支付时间"
        align="center"
        prop="payTime"
        min-width="160" />
     
      
      <el-table-column label="上级会员编号"
        align="center"
        prop="parentNo"
        min-width="120" />
      <el-table-column label="订单状态"
        align="center"
        prop="status"
        min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status"
            :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column label="创建时间"
        align="center"
        prop="createDt"
        min-width="160" />
      <el-table-column label="备注"
        align="center"
        prop="remark"
        min-width="160"
        show-overflow-tooltip />
      <el-table-column label="操作"
        align="center"
        class-name="small-padding fixed-width"
        min-width="240"
        fixed="right">
        <template slot-scope="scope">
          <el-button size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:orders:edit']">修改</el-button>
          <el-button size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:orders:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改商城订单对话框 -->
    <el-dialog :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body>
      <el-form ref="form"
        :model="form"
        :rules="rules"
        label-width="160px">
        <el-form-item label="账套号"
          prop="sysAccount">
          <el-input v-model="form.sysAccount"
            placeholder="请输入账套号" />
        </el-form-item>
        <el-form-item label="订单编号"
          prop="orderNo">
          <el-input v-model="form.orderNo"
            placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="会员ID"
          prop="custId">
          <el-input v-model="form.custId"
            placeholder="请输入会员ID" />
        </el-form-item>
        <el-form-item label="会员编号"
          prop="custNo">
          <el-input v-model="form.custNo"
            placeholder="请输入会员编号" />
        </el-form-item>
        <el-form-item label="会员名称"
          prop="custName">
          <el-input v-model="form.custName"
            placeholder="请输入会员名称" />
        </el-form-item>
        <el-form-item label="实际付款金额"
          prop="amount">
          <el-input v-model="form.amount"
            placeholder="请输入实际付款金额" />
        </el-form-item>
        <el-form-item label="让利金额"
          prop="discount">
          <el-input v-model="form.discount"
            placeholder="请输入让利金额" />
        </el-form-item>
        <el-form-item label="商品总运费 "
          prop="freight">
          <el-input v-model="form.freight"
            placeholder="请输入商品总运费 " />
        </el-form-item>
        <el-form-item label="实收现金"
          prop="receipts">
          <el-input v-model="form.receipts"
            placeholder="请输入实收现金" />
        </el-form-item>
        <el-form-item label="支付时间"
          prop="payTime">
          <el-date-picker v-model="form.payTime"
            type="date"
            placeholder="请选择支付时间"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            style="width: 100%" />
        </el-form-item>
       
        <el-form-item label="上级会员ID"
          prop="parentId">
          <el-input v-model="form.parentId"
            placeholder="请输入上级会员ID" />
        </el-form-item>
        <el-form-item label="上级会员编号"
          prop="parentNo">
          <el-input v-model="form.parentNo"
            placeholder="请输入上级会员编号" />
        </el-form-item>
        <el-form-item label="是否首次购买"
          prop="firstBuy">
          <el-select v-model="form.firstBuy"
            clearable
            placeholder="请选择"
            style="width: 100%">
            <el-option v-for="dict in dict.type.yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)" />
          </el-select>
        </el-form-item>
        <el-form-item label="备注"
          prop="remark">
          <el-input v-model="form.remark"
            placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end">
        <el-button type="primary"
          @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 发货 -->
    <el-dialog title="订单发货"
      :visible.sync="openSendOrder"
      width="500px"
      append-to-body>
      <el-form ref="formSendOrder"
        :model="formSendOrder"
        :rules="rulesSendOrder"
        label-width="110px">
        <el-form-item label="物流单号"
          prop="billNo">
          <el-input v-model="form.billNo"
            placeholder="请输入快递公司名称" />
        </el-form-item>
        <el-form-item label="快递公司名称"
          prop="expCode">
          <el-select v-model="form.expCode"
            clearable
            placeholder="请选择"
            style="width: 100%"
            @change="expCodeChange">
            <el-option v-for="item in expressList"
              :key="item.expCode"
              :label="item.expName"
              :value="item.expCode" />
          </el-select>
        </el-form-item>
      </el-form>

      <div slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end">
        <el-button type="primary"
          @click="submitSendOrder">确 定</el-button>
        <el-button @click="cancelSendOrder">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 查看物流 -->
    <el-dialog title="查看物流"
      :visible.sync="openViewExpress"
      width="600px"
      append-to-body>
      <el-steps direction="vertical"
        :active="0">
        <el-step :title="`${item.Location} (${item.AcceptTime})`"
          :description="item.AcceptStation"
          v-for="(item, idx) in expressInfoList"
          :key="idx"></el-step>
      </el-steps>
      <div slot="footer"
        class="dialog-footer"
        style="display: flex; justify-content: flex-end">
        <!-- <el-button type="primary"
          @click="submitViewExpress">确 定</el-button> -->
        <el-button @click="cancelViewExpress">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listOrders,
  getOrders,
  delOrders,
  addOrders,
  updateOrders,
  setExpNo,
  getExpress
} from "@/api/djk/orderMgr/stockOrder";
import {
  listExpress
} from "@/api/djk/orderMgr/logisticsExpress";

export default {
  name: "Orders",
  dicts: ["order_type", "order_status", "pay_type", "pay_status", "yes_no"],
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商城订单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        orderType: 3,
        orderNo: null,
        custNo: null,
        custName: null,
        custType: null,
        custTypeAfter: null,
        amount: null,
        couponId: null,
        couponMoney: null,
        discount: null,
        freight: null,
        receipts: null,
        refund: null,
        payStatus: 1,
        payTime: null,
        payType: null,
        receiveName: null,
        receiveMobile: null,
        receiveProvince: null,
        receiveCity: null,
        receiveDistrict: null,
        receiveAddress: null,
        parentNo: null,
        firstBuy: null,
        status: null,
        createDt: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      dictData: {},
      // 发货
      expressList: [],
      openSendOrder: false,
      formSendOrder: {},
      rulesSendOrder: {
        billNo: [
          { required: true, message: "物流单号不能为空", trigger: "blur" },
        ],
        expCode: [
          { required: true, message: "请选择物流公司名称", trigger: "change" },
        ],
      },
      // 查看物流
      openViewExpress: false,
      expressInfoList: []
    };
  },
  created () {
    this.getList();
  },

  methods: {
    // 发货
    handleSendOrder (row) {
      this.formSendOrder = {
        id: row.id,
        billNo: null,
        expName: null,
        expCode: null
      }
      this.openSendOrder = true
    },
    expCodeChange (val) {
      this.expressList.forEach(item => {
        if (item.expCode == val) {
          this.formSendOrder.expName = item.expName
        }
      })
    },
    submitSendOrder () {
      this.$refs["formSendOrder"].validate((valid) => {
        if (valid) {
          setExpNo(this.formSendOrder).then((response) => {
            this.$modal.msgSuccess("发货成功");
            this.openSendOrder = false;
            this.getList();
          });
        }
      });
    },
    cancelSendOrder () {
      this.openSendOrder = false;
    },
    // 查看物流
    handleViewExpress (row) {
      getOrders({
        billId: row.billId,
        shipperCode: row.shipperCode,
        logisticCode: row.logisticCode,
        expName: row.expName
      }).then((response) => {
        this.expressInfoList = response.Traces.reverse()
        this.openViewExpress = true
      });
    },
    cancelViewExpress () {
      this.openViewExpress = false
    },
    /** 查询商城订单列表 */
    getList () {
      this.loading = true;
      listOrders(this.queryParams).then((response) => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listExpress({
        pageNum: 1,
        pageSize: 100,
      }).then((response) => {
        this.expressList = response.rows;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        sysAccount: null,
        orderNo: null,
        orderType: null,
        custId: null,
        custNo: null,
        custName: null,
        custType: null,
        custTypeAfter: null,
        amount: null,
        couponId: null,
        couponMoney: null,
        discount: null,
        freight: null,
        receipts: null,
        refund: null,
        payStatus: null,
        payTime: null,
        payType: null,
        receiveName: null,
        receiveMobile: null,
        receiveProvince: null,
        receiveCity: null,
        receiveDistrict: null,
        receiveAddress: null,
        parentId: null,
        parentNo: null,
        firstBuy: null,
        status: null,
        createBy: null,
        createDt: null,
        remark: null,
        version: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加商城订单";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids;
      getOrders(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商城订单";
      });
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除订单编号为"' + ids + '"的数据项？')
        .then(function () {
          return delOrders(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download(
        "web/orders/export",
        {
          ...this.queryParams,
        },
        `orders_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
